﻿using DFGK.IRepository;
using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Common.WebSocketModule;
using DFGK_WMS.Model.Dto;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace DFGK_WMS.WebAPI.Controllers.PDA.入库管理
{
    /// <summary>
    /// 入库管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PDA")]
    public class ReceivingOrderDetailsController : BaseController
    {
        private readonly IReceivingOrderDetailService receivingOrderDetailService;
        private readonly IPutawayOrderRepository putawayOrderRepository;
        private readonly WebSocketConnectManager wsManager;
        private readonly IPalletService palletService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="receivingOrderDetailService"></param>
        /// <param name="palletService"></param>
        /// <param name="putawayOrderRepository"></param>
        /// <param name="wsManager"></param>
        public ReceivingOrderDetailsController(IWebHostEnvironment environment, IReceivingOrderDetailService receivingOrderDetailService, IPalletService palletService, IPutawayOrderRepository putawayOrderRepository, WebSocketConnectManager wsManager) : base(environment)
        {
            this.receivingOrderDetailService = receivingOrderDetailService;
            this.putawayOrderRepository = putawayOrderRepository;
            this.palletService = palletService;
            this.wsManager = wsManager;
        }

        /// <summary>
        /// PDA收货入库
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult BuildCreateStackingStorage(ReceivingBillOrderDto m)
        {
            if (m.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            string message = "";
            m.NowUserId = base.BaseGetNowUserId();
            m.NowUserName = base.BaseGetNowUserName();
            bool IsOK = receivingOrderDetailService.AddReceivingOrderDetail(m, out message);
            if (IsOK)
            {
                var putlist = putawayOrderRepository.GetEntityWhereAll(x => x.Status == 4);
                List<PutPickQuestDto> putPickQuestDtoslist = new List<PutPickQuestDto>();
                if (putlist.Any())
                {
                    foreach (var dto in putlist)
                    {
                        PutPickQuestDto putPickQuestDto = new PutPickQuestDto();
                        putPickQuestDto.Code = dto.Code;
                        if (dto.BillType == 1)
                            putPickQuestDto.BillType = "入库";
                        else
                            putPickQuestDto.BillType = "拣选入库";
                        if (dto.SourceType == 3)
                            putPickQuestDto.BillType = "移库";
                        putPickQuestDto.PalletBarcode = dto.PalletBarcode;
                        putPickQuestDto.LocationCode = dto.LocationCode;
                        putPickQuestDto.BatchNo = dto.BatchNo;
                        putPickQuestDto.Qty = dto.Qty;
                        putPickQuestDto.MaterialCode = dto.MaterialCode;
                        putPickQuestDtoslist.Add(putPickQuestDto);
                    }
                    string liststring = JsonConvert.SerializeObject(putPickQuestDtoslist);
                    wsManager.SendMsgToAllClients(liststring);//给全部连上的客户端发
                }

                return Ok(BaseResult.Result(true, "保存成功！分配的仓位为：" + message, "200"));
            }
            return Ok(BaseResult.Result(false, "保存失败" + message, "500"));
        }

        /// <summary>
        /// 判断托盘是否可用
        /// </summary>
        /// <param name="PalletCode"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult PalletVerify(string PalletCode)
        {
            if (string.IsNullOrEmpty(PalletCode))
            {
                return Ok(BaseResult.Result("400", "托盘不能为空！"));
            }
            string message = "";
            var obj = palletService.PalletVerify(PalletCode, out message);
            if (obj)
            {
                return Ok(BaseResult.Result(true, "", "200", 0));
            }
            return Ok(BaseResult.Result(false, message, "500", 0));
        }

        /// <summary>
        /// 物料入库
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult StartReceivingOrderDetail(StartInventoryDTO m)
        {
            if (string.IsNullOrEmpty(m.PalletCode))
            {
                return Ok(BaseResult.Result("400", "托盘不能为空！"));
            }
            if (string.IsNullOrEmpty(m.ActualLocation))
            {
                return Ok(BaseResult.Result("400", "仓位信息有误！"));
            }
            string message = "";
            m.NowUserId = base.BaseGetNowUserId();
            m.NowUserName = base.BaseGetNowUserName();
            bool result = receivingOrderDetailService.StartReceivingOrderDetail(m, out message);
            if (result)
            {
                return Ok(BaseResult.Result(true, "操作成功", "200"));
            }
            return Ok(BaseResult.Result(false, "操作失败" + message, "500"));
        }


        /// <summary>
        /// 入库查询单据
        /// </summary>
        /// <param name="PalletCode"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetReceivingRoderData(string PalletCode)
        {
            if (string.IsNullOrEmpty(PalletCode))
            {
                return Ok(BaseResult.Result("400", "托盘不能为空！"));
            }
            var obj = receivingOrderDetailService.GetReceivingRoderData(PalletCode, out string message);
            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！" + message, "500", 0));
        }

        /// <summary>
        /// 拣选入库时获取库存信息
        /// </summary>
        /// <param name="palletCode"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPickReceivingsInventoryDto(string palletCode)
        {
            if (string.IsNullOrEmpty(palletCode))
            {
                return Ok(BaseResult.Result("400", "托盘不能为空！"));
            }
            var obj = receivingOrderDetailService.GetPickReceivingsInventoryDto(palletCode, out string message);
            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！" + message, "500", 0));
        }

        /// <summary>
        /// 拣选入库
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult StartPickReceivingOrderDetail(PickReceivingsDto m)
        {
            string message = "";
            bool result = receivingOrderDetailService.StartPickReceivingOrderDetail(m, out message);
            if (result)
            {
                return Ok(BaseResult.Result(true, "操作成功", "200"));
            }
            return Ok(BaseResult.Result(false, "操作失败" + message, "500"));
        }


        [HttpGet]
        public IActionResult SubmitCellIn(string? cellCode)
        {
            if (string.IsNullOrEmpty(cellCode))
            {
                return Ok(BaseResult.Result(false, "查询失败", "200"));
            }
            bool rsult = receivingOrderDetailService.SubmitInventoryIn(cellCode, out string errormsg);
            if (!rsult)
            {
                return Ok(BaseResult.Result(false, "操作失败" + errormsg, "500"));
            }
            return Ok(BaseResult.Result(true, "操作成功", "500"));
        }
    }
}
